package com.vtuzx.kingdee.xqc_api.web;

import com.vtuzx.core.VtuzxException;
import com.vtuzx.core.bean.dto.VtuzxToken;
import com.vtuzx.core.constant.IVtuzxConst;
import com.vtuzx.core.utils.VtuzxUtil;
import com.vtuzx.kingdee.xqc_api.service.IXqcProduceService;
import com.vtuzx.kingdee.xqc_api.service.IXqcSyncKingdeeOrderService;
import com.vtuzx.web.constant.ApiType;
import com.vtuzx.web.validator.VtuzxValidator;
import com.vtuzx.web.web.VtuzxApi;
import com.vtuzx.web.web.VtuzxControlParam;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 同步金蝶订单
 */
@RequestMapping("/${vtuzx.fast.api.path}/${vtuzx.fast.api.version}/sync_kingdee")
@RestController
public class XqcSyncKingdeeOderController {

    /** 生产服务 */
    @Autowired
    private IXqcSyncKingdeeOrderService syncKingdeeOrderService;


    /**
     * 同步采购入库单
     */
    @PostMapping("/sync_pur_in_order")
    @VtuzxApi
    public Object syncPurInOrder(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String source = VtuzxUtil.getString(param, "source");
        syncKingdeeOrderService.syncPurInOrder(_userId, name, billNo, source);
        return null;

    }

    /**
     * 同步采购入库单BN
     */
    @PostMapping("/sync_pur_in_order_bn")
    @VtuzxApi
    public Object syncPurInOrderBn(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String source = VtuzxUtil.getString(param, "source");
        syncKingdeeOrderService.syncPurInOrderBn(_userId, name, billNo, source);
        return null;
    }

    /**
     * 查询同步履历
     */
    @PostMapping("/get_sync_order_history")
    @VtuzxApi
    public Object getSyncOrderHistory(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return syncKingdeeOrderService.getSyncOrderHistory(_userId, name, param);

    }

    /**
     * 订单同步记录外层
     */
    @PostMapping("/get_sync")
    @VtuzxApi
    public Object getSync(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return syncKingdeeOrderService.getSync(_userId, name, param);

    }
    /**
     * 导出订单同步到excle
     */
    @PostMapping("/download_sync_order_history")
    @VtuzxApi(type = ApiType.Download)
    public Object downloadSyncOrderHistory(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        return syncKingdeeOrderService.downloadSyncOrderHistory(token, param);
    }


    /**
     * 导出订单同步根据同步单号)
     */
    @PostMapping("/download_sync_history")
    @VtuzxApi(type = ApiType.Download)
    public Object downloadSyncHistory(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        return syncKingdeeOrderService.downloadSyncHistory(token, param);
    }

    /**
     * 同步其他入库单
     */
    @PostMapping("/sync_other_in_order")
    @VtuzxApi
    public Object syncOtherInOrder(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String source = VtuzxUtil.getString(param, "source");
        syncKingdeeOrderService.syncOtherInOrder(_userId, name, billNo, source);
        return null;
    }

    /**
     * 同步其他出库单
     */
    @PostMapping("/sync_other_out_order")
    @VtuzxApi
    public Object syncOtherOutOrder(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String source = VtuzxUtil.getString(param, "source");
        syncKingdeeOrderService.syncOtherOutOrder(_userId, name, billNo, source);
        return null;
    }

    /**
     * 重推
     */
    @PostMapping("/retry")
    @VtuzxApi( fields = { @VtuzxValidator(key = "id", label = "履历ID", notNull = true, objectId = true) } )
    public Object retry(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> p) throws Exception {
        String id = VtuzxUtil.getString(p, "id");
        syncKingdeeOrderService.retry(token, id);
        return null;
    }



    /**
     * 同步生产订单
     */
    @PostMapping("/sync_prd_order")
    @VtuzxApi
    public Object syncPrdOrder(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String source = VtuzxUtil.getString(param, "source");
        syncKingdeeOrderService.syncPrdOrder(_userId, name, billNo, source,"Invalid Date");
        return null;
    }

    /**
     * 同步生产订单BN
     */
    @PostMapping("/sync_prd_order_bn")
    @VtuzxApi
    public Object syncPrdOrderBn(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String source = VtuzxUtil.getString(param, "source");
        syncKingdeeOrderService.syncPrdOrderBn(_userId, name, billNo, source,"Invalid Date");
        return null;
    }



    /**
     * 同步生产领料单(半成品)
     */
    @PostMapping("/sync_prd_pick_order")
    @VtuzxApi
    public Object syncPrdPickOrder(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String source = VtuzxUtil.getString(param, "source");
        syncKingdeeOrderService.syncPrdPickOrder(_userId, name, billNo, source,null);
        return null;
    }

    /**
     * 同步生产领料单(半成品)BN
     */
    @PostMapping("/sync_prd_pick_order_bn")
    @VtuzxApi
    public Object syncPrdPickOrderBn(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String pickBillNo = VtuzxUtil.getString(param, "pickBillNo");
        String source = VtuzxUtil.getString(param, "source");
        Boolean uploadFlag = VtuzxUtil.getBoolean(param, "uploadFlag");
        syncKingdeeOrderService.syncPrdPickOrderBn(_userId, name, billNo, pickBillNo, source,null, uploadFlag);
        return null;
    }


    /**
     * 同步入库单(半成品)
     */
    @PostMapping("/sync_prd_in_stock_order")
    @VtuzxApi
    public Object syncPrdInStockOrder(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String source = VtuzxUtil.getString(param, "source");
        syncKingdeeOrderService.syncPrdInStockOrder(_userId, name, billNo, source,null);
        return null;
    }

    /**
     * 同步入库单(半成品)
     */
    @PostMapping("/sync_prd_in_stock_order_bn")
    @VtuzxApi
    public Object syncPrdInStockOrderBn(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String source = VtuzxUtil.getString(param, "source");
        syncKingdeeOrderService.syncPrdInStockOrderBn(_userId, name, billNo, source,null);
        return null;
    }

    /**
     * 同步生产订单(分选)
     */
    @PostMapping("/sync_prd_order_sorting")
    @VtuzxApi
    public Object syncPrdOrderSorting(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String source = VtuzxUtil.getString(param, "source");
        String pickTime = VtuzxUtil.getString(param, "pickTime");
        List<Map<String, Object>> boxList = VtuzxUtil.getObject(param, "boxList");
        syncKingdeeOrderService.syncPrdOrderSorting(_userId, name, billNo, source, boxList,pickTime);
        return null;
    }

    /**
     * 同步生产订单(分选)BN
     */
    @PostMapping("/sync_prd_order_sorting_bn")
    @VtuzxApi
    public Object syncPrdOrderSortingBn(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String source = VtuzxUtil.getString(param, "source");
        String pickTime = VtuzxUtil.getString(param, "pickTime");
        List<Map<String, Object>> boxList = VtuzxUtil.getObject(param, "boxList");
        syncKingdeeOrderService.syncPrdOrderSortingBn(_userId, name, billNo, source, boxList,pickTime);
        return null;
    }

    /**
     * 同步生产领料单(分选)
     */
    @PostMapping("/sync_prd_pick_order_sorting")
    @VtuzxApi
    public Object syncPrdPickOrderSorting(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String source = VtuzxUtil.getString(param, "source");
        String pickTime = VtuzxUtil.getString(param,"pickTime");
        List<Map<String, Object>> boxList = VtuzxUtil.getObject(param, "boxList");
        syncKingdeeOrderService.syncPrdPickOrderSorting(_userId, name, billNo, source, boxList,pickTime);
        return null;
    }

    /**
     * 同步生产领料单(分选)BN
     */
    @PostMapping("/sync_prd_pick_order_sorting_bn")
    @VtuzxApi
    public Object syncPrdPickOrderSortingBn(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String source = VtuzxUtil.getString(param, "source");
        String pickTime = VtuzxUtil.getString(param,"pickTime");
        List<Map<String, Object>> boxList = VtuzxUtil.getObject(param, "boxList");
        syncKingdeeOrderService.syncPrdPickOrderSortingBn(_userId, name, billNo, source, boxList,pickTime);
        return null;
    }

    /**
     * 同步生产入库单(分选)
     */
    @PostMapping("/sync_prd_in_stock_order_sorting")
    @VtuzxApi
    public Object syncPrdInStockOrderSorting(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String source = VtuzxUtil.getString(param, "source");
        String prdInTime = VtuzxUtil.getString(param,"prdInTime");
        syncKingdeeOrderService.syncPrdInStockOrderSorting(_userId, name, billNo, source,prdInTime);
        return null;
    }

    /**
     * 同步生产入库单(分选)BN
     */
    @PostMapping("/sync_prd_in_stock_order_sorting_bn")
    @VtuzxApi
    public Object syncPrdInStockOrderSortingBn(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String source = VtuzxUtil.getString(param, "source");
        String prdInTime = VtuzxUtil.getString(param,"prdInTime");
        syncKingdeeOrderService.syncPrdInStockOrderSortingBn(_userId, name, billNo, source,prdInTime);
        return null;
    }

    /**
     * 调拨单(分选)
     */
    @PostMapping("/sync_stock_transfer_order")
    @VtuzxApi
    public Object syncStockTransferOrder(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String source = VtuzxUtil.getString(param, "source");
        String taskTime = VtuzxUtil.getString(param,"taskTime");
        syncKingdeeOrderService.syncStockTransferOrder(_userId, name, billNo, source,taskTime);
        return null;
    }

    /**
     * 调拨单(分选)BN
     */
    @PostMapping("/sync_stock_transfer_order_bn")
    @VtuzxApi
    public Object syncStockTransferOrderBn(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String source = VtuzxUtil.getString(param, "source");
        String taskTime = VtuzxUtil.getString(param,"taskTime");
        syncKingdeeOrderService.syncStockTransferOrderBn(_userId, name, billNo, source,taskTime);
        return null;
    }

    /**
     * 调拨单(调拨)
     */
    @PostMapping("/sync_stock_transfer_order_main")
    @VtuzxApi
    public Object syncStockTransferOrderMain(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String source = VtuzxUtil.getString(param, "source");
        syncKingdeeOrderService.syncStockTransferOrderMain(_userId, name, billNo, source);
        return null;
    }

    /**
     * 生产补料
     */
    @PostMapping("/sync_prd_replenishment")
    @VtuzxApi
    public Object syncPrdReplenishment(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String source = VtuzxUtil.getString(param, "source");
        syncKingdeeOrderService.syncPrdReplenishment(_userId, name, billNo, source,null);
        return null;
    }

    /**
     * 生产补料BN
     */
    @PostMapping("/sync_prd_replenishment_bn")
    @VtuzxApi
    public Object syncPrdReplenishmentBn(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String source = VtuzxUtil.getString(param, "source");
        List<Document> supplementList = new ArrayList<>();
        syncKingdeeOrderService.syncPrdReplenishmentBn(_userId, name, supplementList, source,null);
        return null;
    }

    /**
     * 生产退料
     */
    @PostMapping("/sync_prd_return")
    @VtuzxApi
    public Object syncPrdReturn(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String source = VtuzxUtil.getString(param, "source");
        syncKingdeeOrderService.syncPrdReturn(_userId, name, billNo, source);
        return null;
    }

    /**
     * 销售出库
     */
    @PostMapping("/sync_sale_out")
    @VtuzxApi
    public Object syncSaleOut(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String source = VtuzxUtil.getString(param, "source");
        syncKingdeeOrderService.syncSaleOut(_userId, name, billNo, source);
        return null;
    }


    /**
     * 变品退料
     */
    @PostMapping("/sync_desalting_in_stock")
    @VtuzxApi
    public Object syncDesaltingInStock(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String taskBillNo = VtuzxUtil.getString(param, "taskBillNo");
        String source = VtuzxUtil.getString(param, "source");
        syncKingdeeOrderService.syncDesaltingInStock(_userId, name, taskBillNo, billNo, source,null,0);
        return null;
    }

    /**
     * 变品退料BN
     */
    @PostMapping("/sync_desalting_in_stock_bn")
    @VtuzxApi
    public Object syncDesaltingInStockBn(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String taskBillNo = VtuzxUtil.getString(param, "taskBillNo");
        String source = VtuzxUtil.getString(param, "source");
        syncKingdeeOrderService.syncDesaltingInStockBn(_userId, name, taskBillNo, billNo, source,null,0);
        return null;
    }


    /**
     * 采购入库单
     */
    @PostMapping("/sync_pur_order_in_stock")
    @VtuzxApi
    public Object syncPurOrderInStock(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "otherInBillNo");
        String orderNo = VtuzxUtil.getString(param, "orderNo");
        String source = VtuzxUtil.getString(param, "source");
        syncKingdeeOrderService.syncPurOrderInStock(_userId, name, billNo, orderNo, source);
        return null;
    }

    /**
     * 其它入库和拆箱传批号调整单
     */
    @PostMapping("/sync_transfer_direct_other")
    @VtuzxApi
    public Object syncTransferDirectOther(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String otherInBillNo = VtuzxUtil.getString(param, "otherInBillNo");
        String otherOutBillNo = VtuzxUtil.getString(param, "otherOutBillNo");
        String source = VtuzxUtil.getString(param, "source");
        syncKingdeeOrderService.syncTransferDirectOther(_userId, name, otherInBillNo, otherOutBillNo, source);
        return null;
    }

    /**
     * 采购退货单
     */
    @PostMapping("/sync_pur_order_return")
    @VtuzxApi
    public Object syncPurOrderReturn(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String source = VtuzxUtil.getString(param, "source");
        syncKingdeeOrderService.syncPurOrderReturn(_userId, name, billNo, source);
        return null;
    }


}
